﻿/******************************************************    
文件名:TableSteelCompositionDB.cs
版权  (c)  2010-11-23 淘渺圈圈工作室
作者:黄媛媛
创建日期:2010-12-05
维护人员:黄媛媛
维护日期:2010-12-05
文件功能描述:金属材料热处理网络学习平台_访问TableSteelComposition数据表操作
******************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Data;

namespace Mmht.DB
{
    /// <summary>
    /// 牌号及其化学成分
    /// </summary>
    class TableSteelCompositionDB
    {
        operate op = new operate();
        TableXContentDB tx = new TableXContentDB();

        public string UpdateSteelComposition(DataTable dt, int create_by)
        {
            clearAll();
            int success = 0;
            int failure = 0;
            string message_s = string.Empty;
            string message_f = "第";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                if (insertItem(row, create_by))
                {
                    success++;
                }
                else
                {
                    failure++;
                    message_f += i.ToString() + ",";
                }
            }
            message_s = "牌号及其化学成分数据表共更新数据" + success.ToString() + "条,共计更新失败" + failure.ToString() + "条。";
            //更新x元素
            tx.UpdateXcontent(dt, 1);
            if (failure > 0)
            {
                message_f = message_f.Substring(0, message_f.Length - 1) + "行数据更新失败。";
                return message_s + message_f;
            }
            else
            {
                return message_s;
            }
        }

        private bool clearAll()
        {
            string sql = @"DELETE FROM steel_composition";
            return op.Execute(sql);
        }

        private bool deleteItem(string key)
        {
            string sql = @"DELETE FROM steel_composition WHERE name='" + key + "'";
            return op.Execute(sql);
        }

        private bool insertItem(DataRow row, int create_by)
        {
            SteelComposition s = new SteelComposition();
            s.create_by = create_by;
            s.name = row[0].ToString();
            s.name_en = row[1].ToString();
            s.steel_type = row[2].ToString();
            s.code = row[3].ToString();
            s.remark = row["补充信息描述"].ToString();

            string sql = @"INSERT INTO steel_composition
             (name, name_en, steel_type, code, remark,created_at,viewnum,created_by) VALUES
             ('" + s.name + "','" + s.name_en + "','" + s.steel_type + "','" + s.code + "','" + 
                 s.remark + "','" + s.create_at + "','" + s.viewnum.ToString() + "','" + 
                 s.create_by.ToString() + "')";
            return op.Execute(sql);
        }

        private class SteelComposition
        {
            public string name, name_en, steel_type, code, remark;
            public int create_by;
            public int viewnum = 0;
            public DateTime create_at = DateTime.Now;
        }
    }
}
